package com.qf.servlet;

import com.qf.entity.LivedPet;
import com.qf.service.LivedPetService;
import com.qf.service.impl.LivedPetServiceImpl;
import com.qf.utils.JsonUtils;
import com.qf.utils.PageUtils;
import com.qf.utils.ResultVo;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(name = "livedPetServlet", urlPatterns = "/livedPetServlet")
public class LivedPetServlet extends BaseServlet {
    private LivedPetService livedPetService = new LivedPetServiceImpl();

    /**
     * 查询所有 可分页 模糊
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void servletAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ResultVo<PageUtils<LivedPet>> resultVo = new ResultVo<>();
        PrintWriter pw = resp.getWriter();
        //获取数据
        String type = req.getParameter("type");
        String name = req.getParameter("name");
        String startTime = req.getParameter("startTime");
        String endTime = req.getParameter("endTime");
        String currentPageNo = req.getParameter("currentPageNo");
        String pageSize = req.getParameter("pageSize");
        //调用方法
        PageUtils<LivedPet> livedPets = livedPetService.selectByPage(name, type, startTime, endTime, currentPageNo, pageSize);
        if (livedPets != null) {
            resultVo.setCode(200);
            resultVo.setMessage("查询成功");
            resultVo.setData(livedPets);

        } else {
            resultVo.setCode(500);
            resultVo.setMessage("查询失败");
            resultVo.setData(livedPets);
        }
        JsonUtils.showJson(pw, resultVo);

    }

    /**
     * 增加 name,type,sex非空
     *
     * @param req
     * @param resp
     * @throws ParseException
     * @throws IOException
     */
    public void addLivedPet(HttpServletRequest req, HttpServletResponse resp) throws ParseException, IOException {
        ResultVo<Boolean> resultVo = new ResultVo<>();
        PrintWriter pw = resp.getWriter();
        //获取前端的数据
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String sex = req.getParameter("sex");

        String heightstr = req.getParameter("height");
        int height=-1;
        if (heightstr != null && !"".equals(heightstr)) {
           height = Integer.parseInt(heightstr);
        }

        String weightstr = req.getParameter("weight");
        int weight=-1;
        if (heightstr != null && !"".equals(weightstr)) {
            weight = Integer.parseInt(weightstr);
        }

        String statestr = req.getParameter("state");
        int state=-1;
        if (statestr != null && !"".equals(statestr)) {
            state = Integer.parseInt(statestr);
        }

        String supplyPrice = req.getParameter("supplyPrice");
        String retailPrice = req.getParameter("retailPrice");
        String message = req.getParameter("message");
        //解析时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String birthday = req.getParameter("birthday");
        Date birthdayDate = null;
        if (birthday != null && !"".equals(birthday)) {
            birthdayDate = sdf.parse(birthday);
        }
        String arrivalday = req.getParameter("arrivalday");
        Date arrivaldayDate = null;
        if (arrivalday != null && !"".equals(arrivalday)) {
            arrivaldayDate = sdf.parse(arrivalday);
        }

        String vaccineday = req.getParameter("vaccineday");
        Date vaccinedayDate = null;
        if (vaccineday != null && !"".equals(vaccineday)) {
            vaccinedayDate = sdf.parse(vaccineday);
        }
        String inwormday = req.getParameter("inwormday");
        Date inwormdayDate = null;
        if (inwormday != null && !"".equals(inwormday)) {
            inwormdayDate = sdf.parse(inwormday);
        }
        String outwormday = req.getParameter("outwormday");
        Date outwormdayDate = null;
        if (outwormday != null && !"".equals(outwormday)) {
            outwormdayDate = sdf.parse(outwormday);
        }
        LivedPet livedPet = new LivedPet(-1, name, type, sex, height, weight, state, supplyPrice, retailPrice, message, birthdayDate, arrivaldayDate, vaccinedayDate, inwormdayDate, outwormdayDate);
        int i = livedPetService.add(livedPet);
        if (i > 0) {
            resultVo.setCode(200);
            resultVo.setMessage("增加成功");
            resultVo.setData(true);

        } else {
            resultVo.setCode(500);
            resultVo.setMessage("增加失败");
            resultVo.setData(false);
        }
        JsonUtils.showJson(pw, resultVo);

    }

    /**
     * 修改
     *
     * @param req
     * @param resp
     * @throws ParseException
     * @throws IOException
     */
    public void updateLivedPet(HttpServletRequest req, HttpServletResponse resp) throws ParseException, IOException {
        ResultVo<Boolean> resultVo = new ResultVo<>();
        PrintWriter pw = resp.getWriter();
        //获取前端的数据
        String lidstr = req.getParameter("lid");
        int lid=-1;
        if (lidstr != null && !"".equals(lidstr)) {
            lid = Integer.parseInt(lidstr);
        }else {
                resultVo.setCode(500);
                resultVo.setMessage("lid不能为空");
                resultVo.setData(false);
            JsonUtils.showJson(pw, resultVo);
            return;
        }
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String sex = req.getParameter("sex");
        String heightstr = req.getParameter("height");
        int height=-1;
        if (heightstr != null && !"".equals(heightstr)) {
            height = Integer.parseInt(heightstr);
        }

        String weightstr = req.getParameter("weight");
        int weight=-1;
        if (heightstr != null && !"".equals(weightstr)) {
            weight = Integer.parseInt(weightstr);
        }

        String statestr = req.getParameter("state");
        int state=-1;
        if (statestr != null && !"".equals(statestr)) {
            state = Integer.parseInt(statestr);
        }
        String supplyPrice = req.getParameter("supplyPrice");
        String retailPrice = req.getParameter("retailPrice");
        String message = req.getParameter("message");
        //解析时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String birthday = req.getParameter("birthday");
        Date birthdayDate = null;
        if (birthday != null && !"".equals(birthday)) {
            birthdayDate = sdf.parse(birthday);
        }
        String arrivalday = req.getParameter("arrivalday");
        Date arrivaldayDate = null;
        if (arrivalday != null && !"".equals(arrivalday)) {
            arrivaldayDate = sdf.parse(arrivalday);
        }

        String vaccineday = req.getParameter("vaccineday");
        Date vaccinedayDate = null;
        if (vaccineday != null && !"".equals(vaccineday)) {
            vaccinedayDate = sdf.parse(vaccineday);
        }
        String inwormday = req.getParameter("inwormday");
        Date inwormdayDate = null;
        if (inwormday != null && !"".equals(inwormday)) {
            inwormdayDate = sdf.parse(inwormday);
        }
        String outwormday = req.getParameter("outwormday");
        Date outwormdayDate = null;
        if (outwormday != null && !"".equals(outwormday)) {
            outwormdayDate = sdf.parse(outwormday);
        }
        LivedPet livedPet = new LivedPet(lid, name, type, sex, height, weight, state, supplyPrice, retailPrice, message, birthdayDate, arrivaldayDate, vaccinedayDate, inwormdayDate, outwormdayDate);
        int i = livedPetService.update(livedPet);
        if (i > 0) {
            resultVo.setCode(200);
            resultVo.setMessage("修改成功");
            resultVo.setData(true);
        } else {
            resultVo.setCode(500);
            resultVo.setMessage("修改失败");
            resultVo.setData(false);
        }
        JsonUtils.showJson(pw, resultVo);
    }

     /**
     * 批量删除
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void deleteLivedPet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ResultVo<Boolean> resultVo = new ResultVo<>();
        PrintWriter pw = resp.getWriter();
        //获取数据
        String lid = req.getParameter("lid");
        int i = livedPetService.delete(lid);
        if (i > 0) {
            resultVo.setCode(200);
            resultVo.setMessage("删除成功");
            resultVo.setData(true);

        } else {
            resultVo.setCode(500);
            resultVo.setMessage("删除失败");
            resultVo.setData(false);
        }
        JsonUtils.showJson(pw, resultVo);
    }

      /**
     * 查询指定时间
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void selectByTime(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter pw = resp.getWriter();
        ResultVo<List<LivedPet>> resultVo = new ResultVo<>();
        //获取数据
        String time = req.getParameter("time");
        List<LivedPet> livedPets = livedPetService.selectLivedPetByTime(time);
        if (livedPets != null) {
            resultVo.setCode(200);
            resultVo.setMessage("成功");
            resultVo.setData(livedPets);

        } else {
            resultVo.setCode(500);
            resultVo.setMessage("失败");
            resultVo.setData(livedPets);
        }
        JsonUtils.showJson(pw, resultVo);

    }

}
